from interfaces import *
import ogre.renderer.OGRE as ogre
import ogre.io.OIS as OIS
from world import *
import common_objects as ComObj


class BarrierPole(ITimerUpdatable,ISelectable,IDestroyable):
    
    def getSceneNode(self):
        return self.sceneNode
    
    def getPosition(self):
        if (self.sceneNode):
            return self.sceneNode.Position
        return ogre.Vector3.UNIT_X
    
    def setPosition(self,iP):
        if (self.sceneNode):
            self.sceneNode.Position=iP
    
    Position=property(getPosition,setPosition)
    SceneNode=property(getSceneNode)
    
    def getName(self):
        return self.name
    
    Name=property(getName)
    
    def __init__(self, iWorld,  iName,  iSceneNode, iObstacleMap):
        print "init BP"
        IDestroyable.__init__(self,100)
        self.name=iName
        self.world=iWorld
        self.sceneNode=iSceneNode
        self.obstacleMap=iObstacleMap
        
        self.peers=[]
        self.entity=iWorld.MainWindow.SceneMgr.createEntity(iName, "sphere.mesh")
        self.entity.QueryFlags =World.SELECTABLE_MASK|World.HUMAN_MASK

        self.sceneNode.setScale(0.1, 0.1, 0.1)
        self.sceneNode.attachObject(self.entity)
        self.entity.setVisible = False
        self.billboard = ComObj.GlowingBillboard(self.sceneNode.createChildSceneNode(),iName)
        
    def timerUpdate(self, dt):
    
        if (self.billboard):
            return self.billboard.timerUpdate(dt)
        return True

    def __del__(self):
        del self.billboard
        self.world.MainWindow.SceneMgr.DestroyEntity(self.entity)
        self.world.MainWindow.SceneMgr.DestroySceneNode(self.sceneNode.Name)
        
    def doMouseUp(self, iPos, iButton, isShift, isCtrl, isAlt, iQr, iMO):
        if isAlt and iButton==OIS.MouseButtonID.MB_Right:
            self.world.Destroy(self)
            return True
        
        return False
    
    
class BarrierLink:
    
    def getPeers(self):
        return self.peers
    
    def getName(self):
        return self.name
    
    Name=property(getName)
    Peers=property(getPeers)
    
    def __init__(self,iName, ibp1,ibp2, iWorld):
        self.world=iWorld
        self.name=iName
        
        self.peers=(ibp1,ibp2)
        
        v=ibp1.Position-ibp2.Position
        
        self.sceneNode=iWorld.MainWindow.SceneMgr.RootSceneNode.createChildSceneNode(iName,ibp2.SceneNode.WorldPosition)
        
        self.sceneNode.inheritScale = False
        self.sceneNode.setScale(ogre.Vector3.UNIT_SCALE)
        self.sceneNodeChild = self.sceneNode.createChildSceneNode(v * 0.5)
            
        self.entity=iWorld.MainWindow.SceneMgr.createEntity(iName , "barrierlink")
        self.entity.setMaterialName ( "Alpha/Barrier")
        
        q = ogre.Vector3.UNIT_X.getRotationTo(v.normalisedCopy())

        self.sceneNodeChild.Orientation = q
        self.sceneNodeChild.setScale(v.length() / 32, 1, 1)

        self.sceneNodeChild.attachObject(self.entity)
        
    def isLinking(self,iBP):
        return iBP in self.peers
    
    def __del__(self):
        self.sceneNodeChild.detachAllObjects()
            
        self.sceneNode.detachAllObjects()
        self.world.MainWindow.SceneMgr.destroyEntity(self.entity)
        self.world.MainWindow.SceneMgr.destroySceneNode(self.sceneNodeChild.Name)
        self.world.MainWindow.SceneMgr.destroySceneNode(self.sceneNode.Name)

       